package c_d_user

import (
	"fmt"
	"jihe.common/protos/user_server/proto"
	"jihe.common/tool"
)

func (s *Model) Create(in *proto.UserCDUser) (ret *proto.UserCDUser, err error) {
	ret = new(proto.UserCDUser)
	in.Id = fmt.Sprintf("%d", tool.GetSnowflakeId())
	sqlStr := fmt.Sprintf("INSERT INTO %s (id, d_uid, c_uid)VALUES($1,$2,$3)", s.Name)
	_, err = s.Db.Exec(sqlStr, in.Id, in.DUid, in.CUid)
	if err != nil {
		return
	}
	ret = in
	return
}

func (s *Model) GetCUidsByDUid(in *proto.UserCDUser) (ret *proto.UserGetCDUsersByDUidRes, err error) {
	ret = new(proto.UserGetCDUsersByDUidRes)
	sqlStr := fmt.Sprintf("select c_uid from %s where d_uid = $1", s.Name)
	rows, err := s.Db.Query(sqlStr, in.DUid)
	if err != nil {
		return
	}
	defer rows.Close()
	for rows.Next() {
		cUid := ""
		err = rows.Scan(&cUid)
		if err != nil{
			return
		}
		ret.Uids = append(ret.Uids,cUid)
	}
	return
}



